# $Id: admin.py 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy  <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

from djanet.compartment import models
from djanet.db import netdb
from djanet.utils import admin

class CompartmentAdmin(admin.BaseModelAdmin):
   search_fields = ['name', 'snmp_read', 'enable']

class CompartmentUserAdmin(admin.BaseModelAdmin):
   search_fields = ['username']

class DeviceLoginAdmin(admin.BaseModelAdmin):
   search_fields = ['username', 'device']

class BackupInfoAdmin(admin.BaseModelAdmin):
   search_fields = ['tftp_server', 'device']

class FailoverAdmin(admin.BaseModelAdmin):
   search_fields = ['compartment', 'primary', 'secondary']

class CompartmentAddressAdmin(admin.BaseModelAdmin):
   search_fields = ['compartment', 'ipv4', 'ipv6']

   def formfield_for_foreignkey(self, db_field, request, **kwargs):
      retval = super(CompartmentAddressAdmin, self).formfield_for_foreignkey(
            db_field, request, **kwargs)

      modelopt = None
      if db_field.name == "ipv4":
         modelopt = netdb.Ipblock.objects.filter(version = 4)
      elif db_field.name == "ipv6":
         modelopt = netdb.Ipblock.objects.filter(version = 6)

      if modelopt:
         retval.queryset = modelopt

      return retval

admin.site.register(models.Compartment, CompartmentAdmin)
admin.site.register(models.CompartmentUser, CompartmentUserAdmin)
admin.site.register(models.DeviceLocation)
admin.site.register(models.DeviceLogin, DeviceLoginAdmin)
admin.site.register(models.BackupInfo, BackupInfoAdmin)
admin.site.register(models.Failover, FailoverAdmin)
admin.site.register(models.CompartmentAddress, CompartmentAddressAdmin)
